//
//  FeedCell.m
//  SpendFunk
//
//  Created by bm on 4/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FeedCell.h"
#import "SpendFunkAppDelegate.h"
#import "image.h"
#import "Global.h"

@implementation FeedCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        [self initCellView];
    }
    return self;
}

-(void)initCellView{
	UIView *myContentView = self.contentView;
	//img	
	NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_VIEW_CELL];
	UIImage *img = [UIImage imageNamed:path];
	m_pImgvCell = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, img.size.width, img.size.height)];
	[m_pImgvCell setImage:img];
	[myContentView addSubview:m_pImgvCell];
	[m_pImgvCell release];
	//name
	m_pLblName = [[UILabel alloc] initWithFrame:CGRectMake(8+img.size.width, 0, 200, 30)];
	[m_pLblName setFont:[UIFont fontWithName:@"Arial" size:16]];
	m_pLblName.textAlignment = UITextAlignmentLeft;
	[m_pLblName setTextColor:[UIColor blueColor]];
	[myContentView addSubview:m_pLblName];
	[m_pLblName setBackgroundColor:[UIColor clearColor]];
	[m_pLblName release];
	
	m_pBtnName = [[UIButton alloc] initWithFrame:CGRectMake(8+img.size.width, 0 , 200, 30)];
	[m_pBtnName addTarget:nil action:@selector(onClickBtnName) forControlEvents:UIControlEventTouchUpInside];
	[myContentView addSubview:m_pBtnName];
	[m_pBtnName setBackgroundColor:[UIColor clearColor]];
	[m_pBtnName release];
	// amount
	m_pLblAmount = [[UILabel alloc] initWithFrame:CGRectMake(8+img.size.width ,30, 60, 30)];
	[m_pLblAmount setFont:[UIFont fontWithName:@"Arial" size:16]];
	m_pLblAmount.textAlignment = UITextAlignmentLeft;
	[m_pLblAmount setTextColor:[UIColor blueColor]];
	[myContentView addSubview:m_pLblAmount];
	[m_pLblAmount setBackgroundColor:[UIColor clearColor]];
	[m_pLblAmount release];
	
	
	//locationButton
	m_pLblLocation = [[UILabel alloc] initWithFrame:CGRectMake(8+img.size.width + 50, 30, 200, 30)];
	[m_pLblLocation setFont:[UIFont fontWithName:@"Arial" size:16]];
	m_pLblLocation.textAlignment = UITextAlignmentLeft;
	[m_pLblLocation setTextColor:[UIColor blueColor]];
	[myContentView addSubview:m_pLblLocation];
	[m_pLblLocation setBackgroundColor:[UIColor clearColor]];
	[m_pLblLocation release];
	
	m_pBtnLocation = [[UIButton alloc] initWithFrame:CGRectMake(8+img.size.width + 80, 30, 200, 30)];
	[m_pBtnLocation addTarget:nil action:@selector(onClickBtnLocation) forControlEvents:UIControlEventTouchUpInside];
	[myContentView addSubview:m_pBtnLocation];
	[m_pBtnLocation setBackgroundColor:[UIColor clearColor]];
	[m_pBtnLocation release];
	
	//image Location  
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_LOCATION];
	img = [UIImage imageNamed:path];
	m_pImgvCellLocation = [[UIImageView alloc] initWithFrame:CGRectMake(5, 60, img.size.width, img.size.height)];
	[m_pImgvCellLocation setImage:img];
	[myContentView addSubview:m_pImgvCellLocation];
	[m_pImgvCellLocation release];
	
	//text label    m_pLblDetail
	m_pLblDetail = [[UILabel alloc] initWithFrame:CGRectMake(8+img.size.width, 60, self.frame.size.width - img.size.width-8, 60)];
	[m_pLblDetail setFont:[UIFont fontWithName:@"Arial" size:12]];
	m_pLblDetail.numberOfLines = 10;
	[myContentView addSubview:m_pLblDetail];
	[m_pLblDetail setBackgroundColor:[UIColor clearColor]];
	[m_pLblDetail release];
	
	//comment
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_COMMON];
	img = [UIImage imageNamed:path];
	m_pBtnComment = [[UIButton alloc] initWithFrame:CGRectMake(5, 140, 80, img.size.height)];
	[m_pBtnComment setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnComment.titleLabel setFont:[UIFont fontWithName:@"Arial" size:15]];
	[m_pBtnComment setTitle:@"Comment" forState:UIControlStateNormal];
	[m_pBtnComment setBackgroundColor:[UIColor clearColor]];
	[m_pBtnComment addTarget:nil action:@selector(onClickComment) forControlEvents:UIControlEventTouchUpInside];
	[myContentView addSubview:m_pBtnComment];
	[m_pBtnComment release];
	//like
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_COMMON];
	img = [UIImage imageNamed:path];
	m_pBtnLike = [[UIButton alloc] initWithFrame:CGRectMake(90, 140, 45, img.size.height)];
	[m_pBtnLike setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnLike setTitle:@"Like" forState:UIControlStateNormal];
	[m_pBtnLike.titleLabel setFont:[UIFont fontWithName:@"Arial" size:15]];
	[m_pBtnLike setBackgroundColor:[UIColor clearColor]];
	[m_pBtnLike addTarget:nil action:@selector(onClickLike) forControlEvents:UIControlEventTouchUpInside];
	[myContentView addSubview:m_pBtnLike];
	[m_pBtnLike release];
	
	//m_pLblLike
	m_pLblLike = [[UILabel alloc] initWithFrame:CGRectMake(90, 140, 200, img.size.height)];
	[m_pLblLike setFont:[UIFont fontWithName:@"Arial" size:16]];
	m_pLblLike.textAlignment = UITextAlignmentLeft;
	[m_pLblLike setTextColor:[UIColor blueColor]];
	[myContentView addSubview:m_pLblLike];
	[m_pLblLike setBackgroundColor:[UIColor clearColor]];
	[m_pLblLike release];
	// caculating
	caculating = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100, 140, 20, img.size.height)];
	[caculating setBackgroundColor:[UIColor redColor]];
	caculating.hidden =YES;
	caculating.hidesWhenStopped = YES;
	[myContentView addSubview:caculating];
	[caculating release];
	
	[path release];
}

- (void)layoutSubviews 
{
	[super layoutSubviews];
	/*
	 // getting the cell size
	 CGRect contentRect = self.contentView.bounds;
	 CGFloat boundsX = contentRect.origin.x;
	 CGRect frame;
	 
	 CGFloat leftMargin = 40.f, nDelta = 10;
	 */
}

-(void)onClickComment{
	[g_AppDelegate openCommentView:m_pSpendId];
}

-(void)onClickLike{
	g_iMode = MODE_LIKE;
	m_pBtnLike.hidden = YES;
	caculating.hidden =NO;
	[caculating  startAnimating];
	[self getCountLike];
	gettingCountLike = YES;
	
	//[self addLike];
}
-(void)setAmount:(NSString *)amount{
	m_pLblAmount.text = [NSString stringWithFormat:@"$%@",amount];
}

-(void)setName:(NSString *)name{
	m_pLblName.text = name;
}

-(void)setLocation:(NSString *)location{
	m_pLblLocation.text = location;
}

-(void)setDetailLabel:(NSString *)detail{
	m_pLblDetail.text = detail;
}
- (void)setCity:(NSString*)City{
	if (city) {
		[city release];
		city = nil;
	}
	city = [City retain];
}
-(void)setUserProfile:(NSDictionary *)userProfile{
	m_pUserProfile = [userProfile retain];
}

-(void)onClickBtnName{
	g_iMode = MODE_PROFILE;
	//[self getProfilebyUserid:g_strLoginUserId];
	[g_AppDelegate openUserProfile:m_pUserProfile];
}

-(void)onClickBtnLocation{
	g_iMode = MODE_LOCATION;
	[g_AppDelegate openLocationViewwithFS_ID:m_pFoursquareID gLat:m_pLat gLong:m_pLong aMount:m_pLblAmount.text Address:m_pLblLocation.text City:city];
	//[self getLocationbyFSquareID:m_pFoursquareID];
	//[g_AppDelegate openLocationView];
}

-(void)setSpendId:(NSString*)spendId{
	if (m_pSpendId) {
		[m_pSpendId release];
		m_pSpendId =nil;
	}
	m_pSpendId = [spendId retain];
	NSLog(m_pSpendId);
}
//
- (void)setFourquareID:(NSString *)fsquareID{
	if (m_pFoursquareID) {
		[m_pFoursquareID release];
		m_pFoursquareID =nil;
	}
	m_pFoursquareID = [fsquareID retain];
	NSLog(@"%@",m_pFoursquareID);
}
- (void)setLat:(NSString *)glat{
	if (m_pLat) {
		[m_pLat release];
		m_pLat =nil;
	}
	m_pLat = [glat retain];
}
- (void)setLong:(NSString *)glong{
	if (m_pLong) {
		[m_pLong release];
		m_pLong =nil;
	}
	m_pLong = [[NSString alloc] init];
	m_pLong = [glong retain];
}
#pragma mark like function when click like button
-(void)addLike{
	NSString *post = [NSString stringWithFormat:@"userid=%@&spend_id=%@", g_strLoginUserId, m_pSpendId];
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
	NSURL *url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/like/addlike"];
	
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	if( theConnection )
	{
		m_pWebData = [[NSMutableData data] retain];
		isContinueParse = true;
	}
	else
	{
		
	}
}

-(void)getCountLike{
	NSString *post = [NSString stringWithFormat:@"spend_id=%@", m_pSpendId];
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
	NSURL *url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/like/count_all_like_for_one_spend"];
	
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	if( theConnection )
	{
		m_pWebData = [[NSMutableData data] retain];
		isContinueParse = true;
	}
	else
	{
		
	}
}
#pragma mark getProfilebyUserid
- (void)getProfilebyUserid:(NSString *)userid{
		NSString *post = [NSString stringWithFormat:@"userid=%@", g_strLoginUserId];
		NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
		NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
		NSURL *url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/users/getprofilebyuserid"];
		
		NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
		[theRequest setHTTPMethod:@"POST"];  
		[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
		[theRequest setHTTPBody:postData];	
		
		NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
		if( theConnection )
		{
			m_pWebData = [[NSMutableData data] retain];
			isContinueParse = true;
		}
		else
		{
			
		}
}
- (void)getLocationbyFSquareID:(NSString *)fourSquareID{
	NSString *post = [NSString stringWithFormat:@"four_square_id=%@", fourSquareID];
	NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
	NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
	NSURL *url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/spend/getlocationbyid"];
	
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	if( theConnection )
	{
		m_pWebData = [[NSMutableData data] retain];
		isContinueParse = true;
	}
	else
	{
		
	}
}
#pragma mark delegate connection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[m_pWebData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[m_pWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect to internet" message:@"Please connect to internet!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	
	[alert release];
	[connection release];
	[m_pWebData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
		NSData *loginStatus = [[NSData alloc] initWithBytes: [m_pWebData mutableBytes] length:[m_pWebData length]];
		
		NSString *a = [[NSString alloc] initWithData:loginStatus encoding:NSUTF8StringEncoding];
		NSLog(@"%@",a);
		
		NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:loginStatus];
		[xmlParser setDelegate:self];
		
		BOOL success = [xmlParser parse];
		
		if(success)
		{
			NSLog(@"No Errors");
			int i=1;
			if (g_iMode == MODE_LIKE&& i==1) {
				// set title for cell
				[caculating  stopAnimating];
				[m_pLblLike setText:[NSString	stringWithFormat:@"You and %@ people like this!",countLike]];
				[countLike release];
				//[self addLike];
				i++;
			}
			if (g_iMode == MODE_LOCATION) {
				
			}
			
			
		}
		else
		{
			NSLog(@"Error Error Error!!!");
		}
	[connection release];
	[m_pWebData release];
}

#pragma mark delegate xmlParser
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
	if(!currentElement) 
		currentElement = [[NSString alloc] initWithString:elementName];
	else
		currentElement = [elementName retain];
	
	NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	NSLog(@"Processing Value: %@", string);
	
	if([currentElement isEqualToString:@"status"])
	{
		if([currentElementValue isEqualToString:@"false"])
		{
			
		}
		else
		{
			
		}
	}
	else if([currentElement isEqualToString:@"info"])
	{
		
	}
	if ([currentElement isEqualToString:@"total"]) {
		if (countLike) {
			[countLike release];
			countLike =nil;
		}
		countLike = [[NSString alloc] init];
		countLike = [string	retain];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	
	[currentElement release];
	currentElement = nil;
	[currentElementValue release];
	currentElementValue = nil;
}


- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

